use std::{io, thread};
#[test]
#[allow(unused_variables, dead_code)]
fn test_thread() {
    let _ = process_files_in_parallel(vec!["21321".to_string(),"213213".to_string()]);
}
fn process_files_in_parallel(filenames: Vec<String>) -> io::Result<()> {
    // 把工作拆分成几块
    const NTHREADS: usize = 8;
    let worklists = split_vec_into_chunks(filenames, NTHREADS);
    // 分叉：启动一个线程来处理每一个块
    let mut thread_handles = vec![];
    for worklist in worklists {
        thread_handles.push(thread::spawn(move || process_files(worklist)));
    }
    // 联结：等待所有线程结束
    for handle in thread_handles {
        handle.join().unwrap()?;
    }
    Ok(())
}
fn process_files(filenames: Vec<String>) -> io::Result<()> {
    println!("Processing {} files...", filenames.len());
    Result::Ok(())
}
fn split_vec_into_chunks(filenames: Vec<String>, parts: usize) -> Vec<Vec<String>> {
    if parts == 0 {
        panic!("parts must be positive");
    }
    let mut chunk_size = filenames.len() / parts;
    if parts >= filenames.len() {
        chunk_size = filenames.len()
    }

    let mut chunks = Vec::with_capacity(parts);
    for chunk in filenames.chunks(chunk_size) {
        chunks.push(chunk.to_vec());
    }
    chunks
}